package com.hy.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hy.file.service.FileStorageService;
import com.hy.mapper.AddressMapper;
import com.hy.mapper.HospitalMapper;
import com.hy.pojo.*;
import com.hy.service.IHospitalService;
import com.hy.util.QueryPageBean;
import com.hy.util.QueryPageResult;
import com.hy.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-05
 */
@Service
public class HospitalServiceImpl implements IHospitalService {
    @Autowired
    private HospitalMapper hospitalMapper;
    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private FileStorageService fileStorageService;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public Hospital selectHospitalById(Long id) {
        Hospital hospital = hospitalMapper.selectHospitalById(id);
        Address address = addressMapper.selectById(hospital.getAddressId());
        hospital.setAddress(address);
        return hospital;
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param queryPageBean 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public QueryPageResult selectHospitalList(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<Hospital> hospitals = hospitalMapper.selectHospitalList(queryPageBean);
        for (Hospital hospital : hospitals) {
            hospital.setAddressName(hospital.getAddress().getProvince()+hospital.getAddress().getCity()+hospital.getAddress().getDistrict()+hospital.getAddress().getDetailedAddress());
        }
        PageInfo<Hospital> pageInfo = new PageInfo<>(hospitals);
        return new QueryPageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param hospital 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertHospital(Hospital hospital)
    {
         addressMapper.insert(hospital.getAddress());
        System.err.println(hospital.getAddress().getId());
        hospital.setAddressId(hospital.getAddress().getId());
        hospital.setCreateTime(new Date());
        System.err.println(hospital);
        return hospitalMapper.insertHospital(hospital);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param hospital 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateHospital(Hospital hospital) {
        addressMapper.updateAddress(hospital.getAddress());
        return hospitalMapper.updateHospital(hospital);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteHospitalByIds(Long[] ids) {
        return hospitalMapper.deleteHospitalByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteHospitalById(Long id) {
        return hospitalMapper.deleteHospitalById(id);
    }

    @Override
    public List<Hospital> findHospitals() {
        List<Hospital> list = hospitalMapper.findHospitals();
        return list;
    }

    @Override
    public QueryPageResult findDepByHospitalId(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<BigDep> bigDeps = hospitalMapper.findDepByHospitalId(queryPageBean);
        PageInfo<BigDep> pageInfo = new PageInfo<>(bigDeps);
        return new QueryPageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public String updatefile(MultipartFile file) throws Exception {
        if(file==null || file.getSize()<0){
            return "";
        }
        String s = fileStorageService.uploadImageFile(file.getOriginalFilename(), file.getInputStream());
        return s;
    }

    @Override
    public QueryPageResult findDocByHospitalId(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<Doctors> doctors = hospitalMapper.findDocByHospitalId(queryPageBean);
        PageInfo<Doctors> pageInfo = new PageInfo<>(doctors);
        return new QueryPageResult(pageInfo.getTotal(),pageInfo.getList());
    }
}
